﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Revit;
using SystemDiagramNew.SystemDiagramLegend.DataDefine;
using SystemDiagramNew.TypeDefinition;
using SystemDiagramNew.Utils;

namespace SystemDiagramNew.SystemDiagramLegend
{
	public class LegendEditor
	{
		public LegendEditor(ExternalCommandData cmdData)
		{
			this.m_Revit = cmdData;
		}

		public bool SelectLegendGraphInfos(ref Legend legend)
		{
			return this.SelectGraphObjects(ref legend) && this.GetReferencePoint(ref legend);
		}

		private bool SelectGraphObjects(ref Legend legend)
		{
			IList<Reference> list = null;
			Selection selection = this.m_Revit.Application.ActiveUIDocument.Selection;
			try
			{
				list = selection.PickObjects(Autodesk.Revit.UI.Selection.ObjectType.Element, "选择构成图例的图形（直线、弧线及填充等）");
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				return false;
			}
			Document document = this.m_Revit.Application.ActiveUIDocument.Document;
			List<CurveInfo> list2 = new List<CurveInfo>();
			List<TextNoteInfo> list3 = new List<TextNoteInfo>();
			List<FillRegion> list4 = new List<FillRegion>();
			foreach (Reference reference in list)
			{
				Element element = document.GetElement(reference);
				if (element.GetType() == typeof(DetailLine))
				{
					list2.Add(this.GetLineInfo((element as DetailLine).GeometryCurve));
				}
				else if (element.GetType() == typeof(DetailArc))
				{
					list2.AddRange(this.GeArcInfo((element as DetailArc).GeometryCurve));
				}
				else if (element.GetType() == typeof(ModelLine))
				{
					list2.Add(this.GetLineInfo((element as ModelLine).GeometryCurve));
				}
				else if (element.GetType() == typeof(ModelArc))
				{
					list2.AddRange(this.GeArcInfo((element as ModelArc).GeometryCurve));
				}
				else if (element.GetType() == typeof(TextNote))
				{
					list3.Add(this.GeTextNoteInfo(element as TextNote));
				}
				else if (element.GetType() == typeof(FilledRegion))
				{
					list4.Add(this.GeFillRegionInfo(element as FilledRegion));
				}
			}
			legend.CurveInfoList = list2;
			legend.TextNoteInfoList = list3;
			legend.FillRegionList = list4;
			return true;
		}

		private CurveInfo GetLineInfo(Curve curve)
		{
			Line curve2 = curve as Line;
			EdgeInfo curve3 = new EdgeInfo(curve2.GetEndPoint(0), curve2.GetEndPoint(1), 0.0);
			CurveInfo result = new CurveInfo(curve3, PenColor.PC_Black, 1f);
			return result;
		}

		private List<CurveInfo> GeArcInfo(Curve curve)
		{
			List<CurveInfo> list = new List<CurveInfo>();
			Arc arc = curve as Arc;
			if (!arc.IsBound)
			{
				XYZ center = arc.Center;
				XYZ xyz = XYZ.BasisX * arc.Radius;
				XYZ xyz2 = center + xyz;
				XYZ xyz3 = center - xyz;
				EdgeInfo curve2 = new EdgeInfo(xyz2, xyz3, 1.0);
				CurveInfo item = new CurveInfo(curve2, PenColor.PC_Black, 1f);
				list.Add(item);
				EdgeInfo curve3 = new EdgeInfo(xyz3, xyz2, 1.0);
				CurveInfo item2 = new CurveInfo(curve3, PenColor.PC_Black, 1f);
				list.Add(item2);
				return list;
			}
			XYZ hyendPoint = arc.GetEndPoint(0);
			XYZ hyendPoint2 = arc.GetEndPoint(1);
			bool isAnticlockwise = false;
			if (arc.Normal.IsAlmostEqualTo(XYZ.BasisZ))
			{
				isAnticlockwise = true;
			}
			double bulge = Geometry.GetBulge(hyendPoint, hyendPoint2, arc.Center, isAnticlockwise);
			EdgeInfo curve4 = new EdgeInfo(hyendPoint, hyendPoint2, bulge);
			CurveInfo item3 = new CurveInfo(curve4, PenColor.PC_Black, 1f);
			list.Add(item3);
			return list;
		}

		private TextNoteInfo GeTextNoteInfo(TextNote text)
		{
			string text2 = text.Text;
			TextElementType symbol = text.Symbol;
			double num = symbol.get_Parameter(BuiltInParameter.TEXT_SIZE).AsDouble().FeettoMM();
			double num2 = num * symbol.get_Parameter(BuiltInParameter.TEXT_WIDTH_SCALE).AsDouble();
			BoundingBoxXYZ boundingBoxXYZ = text.get_BoundingBox(this.m_Revit.Application.ActiveUIDocument.ActiveView);
			XYZ center = boundingBoxXYZ.Min + (boundingBoxXYZ.Max - boundingBoxXYZ.Min) / 2.0;
			TextNoteInfo result = new TextNoteInfo(text2, PenColor.PC_Black, num, center, num2 * (double)text2.Length, num);
			return result;
		}

		private FillRegion GeFillRegionInfo(FilledRegion filledRegion)
		{
			List<EdgeInfo> list = new List<EdgeInfo>();
			IList<CurveLoop> boundaries = filledRegion.GetBoundaries();
			foreach (Curve curve in boundaries[0])
			{
				EdgeInfo item = new EdgeInfo(curve);
				list.Add(item);
			}
			FillRegion result = new FillRegion(list, PenColor.PC_Black);
			return result;
		}

		private bool GetReferencePoint(ref Legend legend)
		{
			Selection selection = this.m_Revit.Application.ActiveUIDocument.Selection;
			try
			{
				XYZ position = selection.PickPoint("选择基准点");
				legend.Position = position;
			}
			catch (Exception ex)
			{
				string message = ex.Message;
				return false;
			}
			return true;
		}

		private readonly ExternalCommandData m_Revit;
	}
}
